﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Threading.Tasks;
using LevelEditor.Objects;
using LevelEditor.Objects.Waypoints;
using LevelEditor.Dao;

namespace LevelEditor.Objects.Tiles
{
    public class Waypoint
    {
        public Point location = Point.Empty;
        public int col, row;
        public int idCollection;
        public int idCharacter;
        public Character character;
        public Waypoint next, previous;
        public int idNext, idPrevious;
        public int id;

        public Waypoint() { }
    }

    public class Character : Tile
    {
        // define all member variables. Ths is used to load properties
        // always add the new variable here
        public enum Variables
        {

            PositionX, PositionY, FieldOfView, PerceptionArea, WalkSpeed, MyTrigger, LoneBehavior
        }

        private float fieldOfView;
        public float FieldOfView
        {
            get { return fieldOfView; }
            set { fieldOfView = value; }
        }

        private float perception;
        public float PerceptionArea
        {
            get { return perception; }
            set { perception = value; }
        }

        private float walkSpeed;
        public float WalkSpeed
        {
            get { return walkSpeed; }
            set { walkSpeed = value; }
        }

        private Dictionary<Behavior, WaypointsCollection> waypointsToAction;


        public Character(LevelEditor editor)
            : base(editor)
        {
            this.myType = TileType.Character;
            this.waypointsToAction = new Dictionary<Behavior, WaypointsCollection>();
        }

        public Character() : base()
        {
            this.myType = TileType.Character;
            this.waypointsToAction = new Dictionary<Behavior, WaypointsCollection>();
        }

        public override void Deselect()
        {
            base.Deselect();
            editor.propertiesPanel1.HideProperties(this);
        }

        protected override void Tile_MouseMove(object sender, MouseEventArgs e)
        {
            base.Tile_MouseMove(sender, e);

        }

        protected override void Tile_MouseUp(object sender, MouseEventArgs e)
        {
            base.Tile_MouseUp(sender, e);

            if (Tile.CurrentSelectedTile != null)
                editor.propertiesPanel1.Refresh();
        }

        public void PositionXChanged(object sender, EventArgs args)
        {
            if (((TextBox)sender).Text == "") return;

            Point p = new Point(int.Parse(((TextBox)sender).Text), this.Location.Y);
            this.Location = p;

           this.Father.Tile.Location = p;
        }

        public void PositionYChanged(object sender, EventArgs args)
        {
            if (((TextBox)sender).Text == "") return;

            Point p = new Point(this.Location.X, int.Parse(((TextBox)sender).Text) );
           this.Location = p;

          this.Father.Tile.Location = p;
        }

        public void FieldOfViewChanged(object sender, EventArgs e)
        {
            if (((TextBox)sender).Text == "") return;

            this.fieldOfView = float.Parse(((TextBox)sender).Text);

            ((Character)this.Father.Tile).fieldOfView = this.fieldOfView;

        }

        public void WalkSpeedChanged(object sender, EventArgs e)
        {
            if (((TextBox)sender).Text == "") return;

            this.walkSpeed = float.Parse(((TextBox)sender).Text);

            ((Character)this.Father.Tile).walkSpeed = this.walkSpeed;
        }

        public void PerceptionAreaChanged(object sender, EventArgs e)
        {
            if (((TextBox)sender).Text == "") return;

            this.perception = float.Parse(((TextBox)sender).Text);

            ((Character)this.Father.Tile).perception = this.perception;
        }

        public new Character Clone()
        {
            Character chracter = new Character(this.editor);
            chracter.Name = this.Name;
            chracter.Image = this.Image;
            chracter.fieldOfView = this.fieldOfView;
            chracter.walkSpeed = this.walkSpeed;
            chracter.perception = this.perception;
            chracter.ImageFormat = this.ImageFormat;
            chracter.Width = this.Width;
            chracter.Height = this.Height;

            Point p = new Point(this.Location.X, this.Location.Y);
            chracter.Location = p;

            return chracter;
        }
    }
}
